//
//  GotoCheckViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/10/18.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "GotoCheckViewController.h"
#import "HomeFunTableViewCell.h"
#import "ShopTypeModel.h"
#import "ShopRegisterViewController.h"
#import "GetRegisterInforModel.h"
#import "CheckShopOpenViewController.h"
#import "ShopAndOpenListViewController.h"
#import "OpenAndShopViewController.h"
#import "MerchantManagerViewController.h"
#import "OpenManagerViewController.h"
@interface GotoCheckViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)TB_TableView *homeTableView;
@property(nonatomic,strong)ShopTypeModel *typeModel;
@end

@implementation GotoCheckViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:kColorRGBA(0x2E4361, 1)] forBarMetrics:UIBarMetricsDefault];
    
    if (self.userType==2) {
        
        self.title=@"开办方类型";
        
    }else{
        
        self.title=@"商户类型";
        
    }
    [self.view addSubview:self.homeTableView];
    
    [self loadDatas];
    
    // Do any additional setup after loading the view.
}



-(void)loadDatas
{
 
    [self showLoadingAnimation];
    
    BaseReqApi *Api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/shopType.json" andrequestTime:5 andParams:@{} andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:NO];
    
    [Api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
       
        [self stopLoadingAnimation];
        
        self.typeModel=[ShopTypeModel mj_objectWithKeyValues:responseObject];
        
        if (responseStatus==1) {
            //请求成功
            if (self.typeModel.data.count==0) {
                
                self.Type=TBNODateType;
                
            }else{
                
                [self.homeTableView reloadData];
                
            }
        }else if (responseStatus==0||responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
    
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
            
        }
        
        
    }];

}

-(TB_TableView*)homeTableView
{
    if (!_homeTableView) {
        
        _homeTableView=[[TB_TableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
        _homeTableView.delegate=self;
        _homeTableView.dataSource=self;
        [_homeTableView registerNib:[UINib nibWithNibName:@"HomeFunTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        
         [self configureNeedShowEmptyDataSetScrollView:_homeTableView];
        
        _homeTableView.tableFooterView=[[UIView alloc]init];
        
    }
    
    return _homeTableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.typeModel.data.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 0.1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 10*SCREEN_WIDTH/375.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 90*SCREEN_WIDTH/375.0;
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShopTypeData *data=self.typeModel.data[indexPath.section];
    
    HomeFunTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.Typemodel=data;
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ShopTypeData *data=self.typeModel.data[indexPath.section];
    
    if (self.userType==0) {
        //商户注册
        
        GetRegisterInforModel *shopmodel=[GetRegisterInforModel new];

        shopmodel.type=[NSString stringWithFormat:@"%ld",data.ID];
        
         shopmodel.token=[LoginStatuesModel getUserToken];
        
        if (self.RegisterPeople==1) {
            //开办方帮忙注册
            shopmodel.marketId=[NSString stringWithFormat:@"%ld",[LoginStatuesModel getStaffId]];
            
        }
        
        ShopRegisterViewController *ShopRegister=[ShopRegisterViewController new];
        
        ShopRegister.requrl=@"/index.php/FoodCheckApi/shopRegInfo.json";
        
        ShopRegister.Reqmodel=shopmodel;
        
        ShopRegister.postUrl=@"/index.php/FoodCheckApi/shopReg.json";
        
        ShopRegister.title=@"商户注册";
        
        ShopRegister.BtnStr=@"注册";
        
        ShopRegister.OptionSussStr=@"注册成功";
        
        ShopRegister.block=^()
        {
            
            
        };
        
        [self.navigationController pushViewController:ShopRegister animated:YES];
        
    }else if (self.userType==1)
    {
        //检查员检查去商户或开办方列表

        ShopAndOpenListViewController *gotocheck=[ShopAndOpenListViewController new];

        gotocheck.segmentedArray= [[NSArray alloc]initWithObjects:@"商户",@"开办方",nil];

        gotocheck.LoginType=1;

        gotocheck.Choosetype=[NSString stringWithFormat:@"%ld",data.ID];

        [self.navigationController pushViewController:gotocheck animated:YES];
 
    }else if (self.userType==2)
    {
     //开办方自己注册
        
         GetRegisterInforModel *Openmodel=[GetRegisterInforModel new];
        
        Openmodel.token=[LoginStatuesModel getUserToken];
        
        Openmodel.type=[NSString stringWithFormat:@"%ld",data.ID];
        
        ShopRegisterViewController *OpenRegister=[ShopRegisterViewController new];
     
        OpenRegister.title=@"开办方注册";
        
        OpenRegister.BtnStr=@"注册";
        
        OpenRegister.OptionSussStr=@"注册成功";
        
        OpenRegister.requrl=@"/index.php/FoodCheckApi/marketRegInfo.json";
        
        OpenRegister.Reqmodel=Openmodel;
        
        OpenRegister.postUrl=@"/index.php/FoodCheckApi/marketReg.json";
        
        OpenRegister.block=^()
        {
            
            
        };
    
        [self.navigationController pushViewController:OpenRegister animated:YES];
     
        
    }else if (self.userType==3||self.userType==4)
    {
//        CheckShopOpenViewController *Check=[CheckShopOpenViewController new];
//        Check.segmentedArray= [[NSArray alloc]initWithObjects:@"商户",@"开办方",nil];
//
//        Check.type=data.ID;
//
//        [self.navigationController pushViewController:Check animated:YES];
        
    }
    
    
    
}

- (void)actionTapEmptyDataSetNodataView
{
    [self loadDatas];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
